/***************************************************************************
 创建者: 华磊
 开始时间: 2019.4.12
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:编码器输入模块
 *
 *                                                                         *
 ***************************************************************************/

#ifndef ENCODERRECEIVER_H
#define ENCODERRECEIVER_H

#include "ecattype.h"
#include <QVector>
#include "globalDefine.h"
#include "GeneralDefine.h"

#define D_doOutput_index 0x6600
#define D_doOutput_subindex 0x1

//
#define D_Encoder_Val_index 0x6100
#define D_Encoder0_Val_subindex 0x1
#define D_Encoder1_Val_subindex 0x2
#define D_Encoder2_Val_subindex 0x3

//
#define D_Ltc0_index 0x6300
#define D_HighSpeed_IN0_subindex 0x1
#define D_Ltc0_Finished_subindex 0x2
#define D_Ltc0_Encoder0Val_subindex 0x3
#define D_Ltc0_Encoder1Val_subindex 0x4
#define D_Ltc0_Encoder2Val_subindex 0x5

//
#define D_Ltc1_index 0x6301
#define D_HighSpeed_IN1_subindex 0x1
#define D_Ltc1_Finished_subindex 0x2
#define D_Ltc1_Encoder0Val_subindex 0x3
#define D_Ltc1_Encoder1Val_subindex 0x4
#define D_Ltc1_Encoder2Val_subindex 0x5

//
#define D_Ltc2_index 0x6302
#define D_HighSpeed_IN2_subindex 0x1
#define D_Ltc2_Finished_subindex 0x2
#define D_Ltc2_Encoder0Val_subindex 0x3
#define D_Ltc2_Encoder1Val_subindex 0x4
#define D_Ltc2_Encoder2Val_subindex 0x5

//
#define D_Ltc3_index 0x6303
#define D_HighSpeed_IN3_subindex 0x1
#define D_Ltc3_Finished_subindex 0x2
#define D_Ltc3_Encoder0Val_subindex 0x3
#define D_Ltc3_Encoder1Val_subindex 0x4
#define D_Ltc3_Encoder2Val_subindex 0x5

//
#define D_Cmp0_index 0x6500
#define D_HighSpeed_ReadOut0_subindex 0x1
#define D_Cmp0_FIFO_Exist_subindex 0x2
#define D_Cmp0_Finished_Number_subindex 0x3
#define D_Cmp0_Current_CmpData_subindex 0x4

//
#define D_Cmp1_index 0x6501
#define D_HighSpeed_ReadOut1_subindex 0x1
#define D_Cmp1_FIFO_Exist_subindex 0x2
#define D_Cmp1_Finished_Number_subindex 0x3
#define D_Cmp1_Current_CmpData_subindex 0x4

//
#define D_Cmp2_index 0x6502
#define D_HighSpeed_ReadOut2_subindex 0x1
#define D_Cmp2_FIFO_Exist_subindex 0x2
#define D_Cmp2_Finished_Number_subindex 0x3
#define D_Cmp2_Current_CmpData_subindex 0x4


struct DomainOffset_EncoderReceiver
{
    //pdo
    //0x1600
    unsigned int doOutput;//0x6600 subindex=1 bitLength=16. 3个可用输出。
    //0x1a00
    unsigned int Encoder0_Val;//6010 1 32
    unsigned int Encoder1_Val;//6010 2 32
    unsigned int Encoder2_Val;//6010 3 32
    //0x1a01
    unsigned int HighSpeed_IN0;//6300 1 8
    unsigned int Ltc0_Finished;//6300 2 8
    unsigned int Ltc0_Encoder0Val;//6300 3 32
    unsigned int Ltc0_Encoder1Val;//6300 4 32
    unsigned int Ltc0_Encoder2Val;//6300 5 32

    unsigned int HighSpeed_IN1;//6301 1 8
    unsigned int Ltc1_Finished;//6301 2 8
    unsigned int Ltc1_Encoder0Val;//6301 3 32
    unsigned int Ltc1_Encoder1Val;//6301 4 32
    unsigned int Ltc1_Encoder2Val;//6301 5 32
    //0x1a02
    unsigned int HighSpeed_IN2;//6302 1 8
    unsigned int Ltc2_Finished;//6302 2 8
    unsigned int Ltc2_Encoder0Val;//6302 3 32
    unsigned int Ltc2_Encoder1Val;//6302 4 32
    unsigned int Ltc2_Encoder2Val;//6302 5 32

    unsigned int HighSpeed_IN3;//6303 1 8
    unsigned int Ltc3_Finished;//6303 2 8
    unsigned int Ltc3_Encoder0Val;//6303 3 32
    unsigned int Ltc3_Encoder1Val;//6303 4 32
    unsigned int Ltc3_Encoder2Val;//6303 5 32
    //0x1a03
    unsigned int HighSpeed_ReadOut0;//6500 1 8
    unsigned int Cmp0_FIFO_Exist;//6500 2 16
    unsigned int Cmp0_Finished_Number;//6500 3 16
    unsigned int Cmp0_Current_CmpData;//6500 4 32

    unsigned int HighSpeed_ReadOut1;//6501 1 8
    unsigned int Cmp1_FIFO_Exist;//6501 2 16
    unsigned int Cmp1_Finished_Number;//6501 3 16
    unsigned int Cmp1_Current_CmpData;//6501 4 32

    unsigned int HighSpeed_ReadOut2;//6502 1 8
    unsigned int Cmp2_FIFO_Exist;//6502 2 16
    unsigned int Cmp2_Finished_Number;//6502 3 16
    unsigned int Cmp2_Current_CmpData;//6502 4 32
};

//class RobotControl;

class EncoderReceiver
{
public:
    EncoderReceiver(E_DEVICE_PROTOCOL protocolIn,int aliasIn,  QString configFileIn);

public:
    int getEncoderReceiverOutput(unsigned short &doOut);
    int setEncoderReceiverOutput(unsigned short doIn);
    int setEncoderReceiverOutput_bit(int bitIndex, bool isHigh);
    int getEncoderReceiverData(EncoderReceiverData &dataOut);
    int setVendorId(int vendorIdIn);
    bool  isInitialOkStatus();


private:
    int checkMultiVerse();//检查多圈溢出. not used
    int readConfigFile(  QString filepath );
    int translateIntToLonglong(const int &valueNewIn,int &valueOldIn,int &count,long long &valueOut);

public:
    QVector<SdoConfig> sdoConfigList;
    bool isInitialOk;
    EncoderReceiverData pdoData;
//    //pdo
//    //0x1600
//    unsigned short doOutput;//0x6600 subindex=1 bitLength=16. 3个可用输出。
//    //0x1a00
//    int Encoder0_Val;//6010 1 32
//    int Encoder1_Val;//6010 2 32
//    int Encoder2_Val;//6010 3 32
//    //0x1a01
//    unsigned char HighSpeed_IN0;//6300 1 8
//    unsigned char Ltc0_Finished;//6300 2 8
//    int Ltc0_Encoder0Val;//6300 3 32
//    int Ltc0_Encoder1Val;//6300 4 32
//    int Ltc0_Encoder2Val;//6300 5 32

//    unsigned char HighSpeed_IN1;//6301 1 8
//    unsigned char Ltc1_Finished;//6301 2 8
//    int Ltc1_Encoder0Val;//6301 3 32
//    int Ltc1_Encoder1Val;//6301 4 32
//    int Ltc1_Encoder2Val;//6301 5 32
//    //0x1a02
//    unsigned char HighSpeed_IN2;//6302 1 8
//    unsigned char Ltc2_Finished;//6302 2 8
//    int Ltc2_Encoder0Val;//6302 3 32
//    int Ltc2_Encoder1Val;//6302 4 32
//    int Ltc2_Encoder2Val;//6302 5 32

//    unsigned char HighSpeed_IN3;//6303 1 8
//    unsigned char Ltc3_Finished;//6303 2 8
//    int Ltc3_Encoder0Val;//6303 3 32
//    int Ltc3_Encoder1Val;//6303 4 32
//    int Ltc3_Encoder2Val;//6303 5 32
//    //0x1a03
//    unsigned char HighSpeed_ReadOut0;//6500 1 8
//    unsigned short Cmp0_FIFO_Exist;//6500 2 16
//    unsigned short Cmp0_Finished_Number;//6500 3 16
//    int Cmp0_Current_CmpData;//6500 4 32

//    unsigned char HighSpeed_ReadOut1;//6501 1 8
//    unsigned short Cmp1_FIFO_Exist;//6501 2 16
//    unsigned short Cmp1_Finished_Number;//6501 3 16
//    int Cmp1_Current_CmpData;//6501 4 32

//    unsigned char HighSpeed_ReadOut2;//6502 1 8
//    unsigned short Cmp2_FIFO_Exist;//6502 2 16
//    unsigned short Cmp2_Finished_Number;//6502 3 16
//    int Cmp2_Current_CmpData;//6502 4 32

    //sdo


    DomainOffset_EncoderReceiver domainOffset;
    int vendorId;
    int alias;
};

#endif // ENCODERRECEIVER_H
